### Measuring Political Attitudes with Word Association - Convergence Validity ###
## Ze Han, Ph.D. Student, Princeton University Department of Politics, zeh@princeton.edu ##
## Naijia Liu, Assistant Professor, Harvard University Department of Government, naijialiu@fas.harvard.edu ##
## Rory Truex, Associate Professor, Princeton University Department of Politics and School of Public and International Affairs, rtruex@princeton.edu ##

setwd("~/Desktop/WAT_PoQ/code")

## Load Packages ##
rm(list=ls(all=TRUE))

library(tidyverse)
library(hrbrthemes)
library(viridis)

## Load Datasets ##
data.wat.filt <- read.csv("./data/data.wat.filt.csv") %>% 
  select(ResponseID, term.eng, sat.central, sat.local, sat.central.pol)

sentiment <- read.csv("./data/sentiment.csv") %>% 
  select(-tokens, -text) %>% 
  left_join(data.wat.filt, by = c("ResponseID", "term.eng")) %>% 
  mutate(term.eng = str_replace_all(term.eng,
                                    c("central government" = "Central Government",
                                      "democracy" = "Democracy"))) %>% 
  filter(term.eng == "Central Government" | term.eng == "CCP")

# Figure 5: Sentiment Scores by Satisfaction with Central Government (Mainland China) - Filtered
p1 <- sentiment %>% 
  group_by(sat.central, term.eng) %>%
  filter(!is.na(sat.central)) %>% 
  summarise(mean_sentiment = mean(sentiment, na.rm = TRUE)) %>% 
  ggplot(aes(x = sat.central, y = mean_sentiment, color = term.eng)) +
  geom_line() +
  geom_point() +
  ylim(0.2, 1) +
  scale_x_continuous(breaks = seq(1, 10, by = 1)) +
  labs(x = "Satisfaction with Central Government",
       y = "Sentiment Score",
       color = "Cue") +
  scale_color_viridis(discrete = TRUE) +
  theme_ipsum() 
ggsave("./figures/fig-conv-st-central.png", p1, width = 8, height = 6)

# Figure SI7: Sentiment Scores by Satisfaction with Local Government (Mainland China) - Filtered
p2 <- sentiment %>% 
  group_by(sat.local, term.eng) %>%
  filter(!is.na(sat.local)) %>% 
  summarise(mean_sentiment = mean(sentiment, na.rm = TRUE)) %>% 
  ggplot(aes(x = sat.local, y = mean_sentiment, color = term.eng)) +
  geom_line() +
  geom_point() +
  ylim(0.4, 1) +
  scale_x_continuous(breaks = seq(1, 10, by = 1)) +
  labs(x = "Satisfaction with Local Government",
       y = "Sentiment Score",
       color = "Cue") +
  scale_color_viridis(discrete = TRUE) +
  theme_ipsum() 
ggsave("./figures/fig-conv-st-local.png", p2, width = 8, height = 6)

# Figure SI8: Sentiment Scores by Satisfaction with Government Policy (Mainland China) - Filtered
p3 <- sentiment %>% 
  group_by(sat.central.pol, term.eng) %>%
  filter(!is.na(sat.central.pol)) %>% 
  summarise(mean_sentiment = mean(sentiment, na.rm = TRUE)) %>% 
  ggplot(aes(x = sat.central.pol, y = mean_sentiment, color = term.eng)) +
  geom_line() +
  geom_point() +
  ylim(0.5, 1) +
  scale_x_continuous(breaks = seq(1, 5, by = 1)) +
  labs(x = "Satisfaction with Government Policy",
       y = "Sentiment Score",
       color = "Cue") +
  scale_color_viridis(discrete = TRUE) +
  theme_ipsum() 
ggsave("./figures/fig-conv-st-gov-policy.png", p3, width = 8, height = 6)